package org.zjt.util;


import io.netty.channel.ChannelFuture;
import org.springframework.util.StringUtils;
import org.zjt.domain.RegisteNodeData;
import org.zjt.domain.RpcMsgProto;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

import static org.zjt.util.RpcContext.LoadBalanceProtocol.RADOM;
import static org.zjt.util.RpcContext.LoadBalanceProtocol.REACTIME;
import static org.zjt.util.RpcContext.LoadBalanceProtocol.ROTAION;


/**
 * Created by Administrator on 2017/4/15.
 */
public class RpcContext {
    public static final ConcurrentHashMap<String, RpcMsgProto.msg> RPC_TRAMCE_MAP = new ConcurrentHashMap();
    public static final ConcurrentHashMap<String, CountDownLatch> RPC_TRAMCE_STATUS = new ConcurrentHashMap();
    public static final ConcurrentHashMap<String, ChannelFuture> RPC_NETTY_CLIENTS = new ConcurrentHashMap();
    public static volatile LoadBalanceProtocol LoadBalance = null;

    public static volatile RegisteNodeData currentServiceNode = null;

    public static  LoadBalanceProtocol getLoadBalanceProtocol() {
        if (LoadBalance == null)
            synchronized (RpcContext.class){
                if (LoadBalance == null)
                    LoadBalance = ROTAION ;
            }
        return LoadBalance;
    }

    public static  void initLoadBalance(String var1) {
        if (StringUtils.isEmpty(var1)){
            LoadBalance = ROTAION ;
            return;
        }
        switch (var1){
            case "RADOM": LoadBalance = RADOM; break;
            case "REACTIME": LoadBalance = REACTIME; break;
            case "ROTAION": LoadBalance = ROTAION; break;
            default:LoadBalance = ROTAION ;
        }
    }

    enum LoadBalanceProtocol {
        RADOM("随机访问", 1), REACTIME("最小时延", 2), ROTAION("轮询", 3);
        private String name;
        private int protocol;
        LoadBalanceProtocol(String name, int protocol) {
            this.name = name;
            this.protocol = protocol;
        }
    }
}
